<template>
    <div class="">
      <div id="header" class="fixed-header">
        <div id="header_nav">
          <div id="header_nav1">
            <img id="nav1-img" src="https://course.myhope365.com/img/logo-text-green.2137bfaa.png" alt="">
            <div id="nav_left">
              <ul>
                <li id="nav_index" style="float: left; list-style: none; margin-left: 20px; margin-top: 20px;">
                  <a href="" style="text-decoration: none; color:black; font-size: 19px; font-weight: bold;">首页</a>
                </li>
                <li class="headerdetails" style="float: left; list-style: none; margin-left: 20px; margin-top: 20px;">
                  <a href="" id="header-coursesort"
                    style="text-decoration: none; color:black; font-size: 19px; font-weight: bold;"
                    @mouseenter="toggleList(true)" @mouseleave="toggleList(false)">课程分类 ⌵</a>  
                  <div id="courseDetail" v-show="isShow" @mouseenter="toggleList(true)" @mouseleave="toggleList(false)">
                    <ul>
                      <li id="detail" v-for="item in courseDetail" :key="item.subjectId">
                        <a href=""> {{ item.title }}</a>
                      </li>
                    </ul>
                  </div>                                      
                </li>
                <li id="nav-plat" style="float: left; list-style: none; margin-left: 20px; margin-top: 20px;">
                  <a href="https://stu.myhope365.com/home"
                    style="text-decoration: none; color:black; font-size: 19px; font-weight: bold;">
                    教学管理云平台
                  </a>
                </li>
              </ul>
            </div>
            <div id="search">
              <input 
                type="text" 
                placeholder="搜索课程~" 
                v-model="searchKeyword"
                @keyup.enter="handleSearch"
              >
              <button id="search-button" @click="handleSearch">
                <img src="" alt="">
              </button> 
            </div>
            <div id="login">
                    <span><img @click="ToCart" src="" alt=""> </span>
                    <LoginView />

                </div>
          </div>
        </div>
      </div>
    </div>
  </template>
  
  <script>
  import { getDetail } from "@/api/request";
  import LoginView from "@/components/loginView.vue";
  
  export default {
    name:'HeaderView',
    data() {
      return {
        isShow: false,
        courseDetail: [],
        searchKeyword: ''
      }
    },
    components: {
      LoginView, 
    },
    created() {
      getDetail({"enable": "1"}).then(res => {
        this.courseDetail = res.rows
      });
      
      // 从路由中获取关键字并设置到搜索框
      if (this.$route.path === '/search' && this.$route.query.keyword) {
        this.searchKeyword = this.$route.query.keyword;
      }
    },
    watch: {
      // 监听路由变化，当从搜索结果页返回时更新搜索框
      '$route'(to) {
        if (to.path === '/search' && to.query.keyword) {
          this.searchKeyword = to.query.keyword;
        } else {
          this.searchKeyword = '';
        }
      }
    },
    methods: {
      ToCart() {
        this.$router.push({ path: '/cart' }); 
      },
      handleSearch() {
        if (!this.searchKeyword.trim()) {
          if (this.$route.path === '/search') {
            this.$router.push({ path: '/search' });
          }
          return;
        }
        if (this.$route.path === '/search' && 
            this.$route.query.keyword === this.searchKeyword.trim()) {
          return;
        }
  
        this.$router.push({
          path: '/search',
          query: {
            keyword: this.searchKeyword.trim()
          }
        });
      },
      toggleList(visible) {
        this.isShow = visible;
      },
    }
  }
  </script>

<style lang="scss" scoped>
#header {
    width: 100%;
    height: 80px;
    background-color: white;
    #header_nav {
        height: 80px;

        #header_nav1 {
            display: flex;
            width: 100%;
            height: 80px;
            margin: auto;
            #nav1-img{
                width: 132;
                height: 40px;
                margin: auto 100px;
            }
            #nav_left {
               
                height: 100%;
            }
            #search {
                display: flex;
                margin-top: 20px;
                margin-left: 150px;
                position: relative;

                input {
                    width: 270px;
                    height: 40px;
                    border-radius: 15px 0 0 15px;
                    border: none;
                    outline: none;
                    border: 1px solid #d7d3d3;
                    padding: 0 15px;
                    font-size: 14px;
                }

                button {
                    display: flex;
                    justify-content: center;
                    align-items: center;
                    width: 40px;
                    height: 40px;
                    border-radius: 0 15px 15px 0;
                    border: 1px solid #fff;
                    background-color: rgb(0,178,120);
                    cursor: pointer;
                    img{
                        width: 20px;
                        height: 20px;
                        margin-left: 5px;
                    }
                }
            }
        }
    }
}

#login {
    display: flex;
    justify-content: center;
    align-items: center;
    margin-left: 20px;

    span {
        background-color: #fff;
        border-radius: 50px;
        width: 36px;
        height: 36px;
        img{
            margin: 10px;
        }
    }
    #show-login-btn {
        width: 100px;
        height: 30px;
        background-color: #fff;
        margin-left: 20px;
        padding-left: 10px;
        border-radius: 15px;
        text-decoration: none;
        color: black;
        font-size: 15px;
        font-weight: bold;
        line-height: 30px;
        text-align: center;
        display: block;
        box-shadow: 10px 10px 10px rgba(0, 0, 0, 0.3);
        cursor: pointer;
    }
}
#nav_index {
    margin-left: 20px; 
}
.headerdetails{
    position: relative;
}
#courseDetail{
    background-color: white;
    width: 140px;
    border: 1px solid #c6cbcc;
    position:absolute;
    z-index: 999;
    margin-left: -20px;
    padding: 5px 0; 
    ul{
        padding:0;
    }
}

#detail {
    list-style: none;
    cursor: pointer;
    height: 30px;
   width: 100%;
    
    a {
    display: block; 
    padding: 5px 10px; 
    text-align: left; 
    line-height: 20px;
    font-size: 14px;
    text-decoration: none;
    color: #555;
    }
}
#detail:hover{
    background-color: #def1e6;
}
.search-input::placeholder {
  padding-left: 16px; 
}
</style>